const USER = require('../../models/user.model')
const {RESPONSE_CLIENT,MD5,} = require('../../utils/tools')


module.exports = {
 login:{
   api:'/admin/login',
   type:'post',
   method:(req,res)=>{
    const {name,password}=req.body
    if (!name||!password) {
      RESPONSE_CLIENT(res,200,400,'用户名或密码错误')
    }
    const cryptoPwd=MD5(password)
    USER.findOne({name,password:cryptoPwd}).then(function(userInfo){
      if (userInfo._id) {
        req.session.userInfo=userInfo
        RESPONSE_CLIENT(res,200,200,'登录成功',null)
      }else{
       RESPONSE_CLIENT(res,200,402,'用户名或密码错误',null)
      }
    }).catch((err) => {
      RESPONSE_CLIENT(res)
    })
  }
 },
 logout:{
  api:'/admin/logout',
   type:'post',
   method:(req,res)=>{
    if (req.session.userInfo) {
      req.session.userInfo=null
      RESPONSE_CLIENT(res,200,200,'退出登录成功')
    } else {
      RESPONSE_CLIENT(res,200,402,'当前未登录')
    }
   }
 },
 currentuser:{
  api:'/admin/currentuser',
   type:'get',
   method:(req,res)=>{
    if (req.session.userInfo) {
      RESPONSE_CLIENT(res,200,200,'获取成功',req.session.userInfo)
    }else{
      RESPONSE_CLIENT(res,200,403,'登录失效,请重新登陆',null)
    }
   }
 }
}
